package leetcode.code1727;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int largestSubmatrix(int[][] matrix) {
		int m = matrix.length, n = matrix[0].length;
		int[] sums = matrix[0];
		int ans = this.max(sums);
		for (int i = 1; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (matrix[i][j] == 1) {
					sums[j] += 1;
				} else {
					sums[j] = 0;
				}
			}
			ans = Math.max(ans, this.max(sums));
		}
		return ans;
	}

	private int max(int[] sums) {
		int[] sort = sums.clone();
		Arrays.sort(sort);
		int len = sort.length;
		int ans = Integer.MIN_VALUE;
		for (int i = len - 1; i >= 0; i--) {
			int size = len - i;
			int cur = sort[i];
			ans = Math.max(ans, size * cur);
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(4, this.largestSubmatrix(H.str2array1("[[0,0,1],[1,1,1],[1,0,1]]")));
		H.compare(3, this.largestSubmatrix(H.str2array1("[[1,0,1,0,1]]")));
		H.compare(2, this.largestSubmatrix(H.str2array1("[[1,1,0],[1,0,1]]")));
		H.compare(0, this.largestSubmatrix(H.str2array1("[[0,0],[0,0]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
